package com.pdsu.alumni.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pdsu.alumni.common.result.Result;
import com.pdsu.alumni.pojo.MessageType;
import com.pdsu.alumni.service.IMessageTypeService;
import com.pdsu.alumni.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Acerola
 * @since 2021-11-05
 */
@Api(tags = "新闻类别管理")
@RestController
@RequestMapping("/message-type")
public class MessageTypeController {
    @Autowired
    private IMessageTypeService messageTypeService;
    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation(value = "获取所有新闻类别")
    @GetMapping("/getList")
    @RequiresAuthentication
    @RequiresPermissions("message")
    @ResponseBody
    @CrossOrigin
    public Result<?> getAllMessageType() {
        List<MessageType> tree = messageTypeService.Tree();
        return new Result<>().success().put(tree);
    }

    @ApiOperation(value = "新增新闻类别")
    @PostMapping("/addMessageType")
    @RequiresAuthentication
    @RequiresPermissions("message")
    @ResponseBody
    @CrossOrigin
    public Result<?> addMessageType(@RequestBody Map map) {
        Object res = map.get("map");
        boolean b = true;
        MessageType messageType = JSON.parseObject(JSON.toJSONString(res), MessageType.class);
        //判断重复
        List<MessageType> list = messageTypeService.list();
        for (MessageType type : list) {
            if (type.getTypeName().equals(messageType.getTypeName())) {
                b = false;
            }
        }
        if (!b) {
            return new Result<>().success(999, "分类重复");
        } else {
        }
        messageTypeService.save(messageType);
        //傻逼电脑
        //造path!
        MessageType messageType1 = messageTypeService.getOne(new QueryWrapper<MessageType>().eq("type_name", messageType.getTypeName()));
        String path = null;
        if (messageType1.getTypeParentId() == 0) {
            path = "/" + messageType1.getId();
        } else {
            path = "/" + messageType1.getTypeParentId() + "/" + messageType1.getId();
        }

        messageType1.setTypePath(path);
        messageTypeService.updateById(messageType1);
        return new Result<>().success("添加成功");

    }

    @ApiOperation(value = "修改新闻类别")
    @PostMapping("/updateMessageType")
    @RequiresAuthentication
    @RequiresPermissions("message")
    @ResponseBody
    @CrossOrigin
    public Result<?> updateMessageType(@RequestBody Map map) {
        Object res = map.get("map");
        boolean b = true;
        MessageType messageType = JSON.parseObject(JSON.toJSONString(res), MessageType.class);
        //判断重复
        List<MessageType> list = messageTypeService.list();
        for (MessageType type : list) {
            if (type.getTypeName().equals(messageType.getTypeName())) {
                b = false;
            }
        }
        if (!b) {
            return new Result<>().success(999, "分类重复");
        } else {
            messageTypeService.updateById(messageType);
            return new Result<>().success("修改成功");
        }
    }

    @ApiOperation(value = "根据id获取新闻类别内容")
    @GetMapping("/getMessageTypeById")
    @RequiresAuthentication
    @RequiresPermissions("message")
    @ResponseBody
    @CrossOrigin
    public Result<?> getMessageTypeById(Integer id) {
        MessageType messageType = messageTypeService.getById(id);
        return new Result<>().success().put(messageType);
    }

//    @ApiOperation(value = "根据当前用户角色获取分类")
//    @GetMapping("/getRoleTypeList")
//    @RequiresAuthentication
//    @RequiresPermissions("message")
//    @ResponseBody
//    @CrossOrigin
//    public Result<?> getRoleTypeList(@CurrentUser User user) {
//        //查询当前用户角色
//        List<Role> roles = userRoleService.queryRolesByUserId(user.getUserId());
//        Role role = roles.get(0);
//        Map<String, Object> tree = messageTypeService.findTreeByRole(role);
//        return new Result<>().success().put(tree);
//    }


}
